/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.dao.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cx1.dao.api.IEmailInviteeDao;
import org.cx1.model.utils.EmailInvitee;

/**
 *
 * @author ECH
 */
public class EmailInviteeDao implements IEmailInviteeDao {

    private SqlMapClient sqlMapClient;

    public EmailInviteeDao() {
        sqlMapClient = IbatisConnectionFactory.getSqlMapClient();
    }
    
    @Override
    public List<EmailInvitee> getAll() {
        List<EmailInvitee> emailInvitees = new ArrayList<EmailInvitee>();        
        try {
            emailInvitees = sqlMapClient.queryForList("emailInvitee.getAll");
        } catch (Exception ex) {
            ex.printStackTrace();
        }        
        return emailInvitees;
    }

    @Override
    public List<EmailInvitee> getAll(int offset, int limit) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String add(EmailInvitee emailInvitee) {
        String email = "";        
        try {
            sqlMapClient.insert("emailInvitee.add", emailInvitee);
            email = emailInvitee.getEmail();            
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }        
        return email;
    }

    @Override
    public int update(EmailInvitee emailInvitee) {
        int noOfRowsAffected = 0;        
        try {
            noOfRowsAffected = sqlMapClient.update("emailInvitee.update", emailInvitee);
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }        
        return noOfRowsAffected;
    }

    @Override
    public int deleteByEmail(String email) {
        int noOfRowsAffected = 0;        
        try {
            noOfRowsAffected = sqlMapClient.delete("emailInvitee.deleteByEmail", email);
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }             
        return noOfRowsAffected;
    }

    @Override
    public int setProcessed(String email, boolean processed) {
        int noOfRowsAffected = 0;        
        try {
            Map param = new HashMap();
            param.put("email", email);
            param.put("processed", processed);
            
            noOfRowsAffected = sqlMapClient.update("emailInvitee.setProcessed", param);
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }        
        return noOfRowsAffected;
    }

    @Override
    public List<EmailInvitee> getAll(boolean processed, int offset, int limit) {        
        List<EmailInvitee> emailInvitees = new ArrayList<EmailInvitee>();        
        try {
            Map param = new HashMap();
            param.put("processed", processed);
            param.put("offset", offset);
            param.put("limit", limit);
            
            emailInvitees = sqlMapClient.queryForList("emailInvitee.getAllByProcessed", param);
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }        
        return emailInvitees;
    }
    
}
